<html>
<head>
<title>SENSORNET - pilot server</title>

<!-- CALENDAR -->
<script language="javascript" src="../datagraph/calendar/calendar.js"></script>
</head>

<body>

<!-- GLOBAL CONFIGURATION -->
<?php 

require_once ('./calendar/tc_calendar.php');
require_once ('sn_functions.php');
require_once ('gChart.php');

$CALENDAR_FOLDER='./calendar';
?>

<!-- Interval selection section-->
<h4>Please, select the node and date interval:</h4>

<form name="select_interval_form" method="GET"><span>Node:</span>

<select name="sensors">';
<?php
  $nodes = get_sensor_nodes();
  foreach ( $nodes as $key => $value) {
    echo '<option value="' . $key . '"';
    if ( $_GET["sensors"] == $key ) echo ' selected="selected" ';
    if ($_GET["sensors"] == '' && $key == 'SEN003') echo ' selected="selected" ';
    echo  '>' . $value . '</option>';
  }   
  ?>
</select>
<br />
<br />

<span>Initial date:</span> 
<?php
$url = curPageURL();
$myCalendar = new tc_calendar("date1", true);
$myCalendar->setIcon($CALENDAR_FOLDER . '/images/iconCalendar.gif');
$myCalendar->setPath($CALENDAR_FOLDER);

$url_date1 = get_URL_date1($url);
if($url_date1["year"] == null && $url_date1["month"] == null && $url_date1["day"]== null){
	$calculated_data = calculate_data(date('d'), date('m'), date('Y'));
	$myCalendar->setDate($calculated_data["day"], $calculated_data["month"], $calculated_data["year"]);
}
else{
	$myCalendar->setDate($url_date1["day"], $url_date1["month"], $url_date1["year"]);
}

$myCalendar->setYearInterval(2010, 2020);
$myCalendar->dateAllow('2010-01-01', '2020-12-31', false);
$myCalendar->startMonday(true);
$myCalendar->writeScript();
?> 

<span>End date:</span> 
<?php
$myCalendar = new tc_calendar("date2", true);
$myCalendar->setIcon($CALENDAR_FOLDER . '/images/iconCalendar.gif');
$myCalendar->setPath($CALENDAR_FOLDER);
$a = curPageURL();

$url_date2 = get_URL_date2($url);
if($url_date2["year"] == null && $url_date2["month"] == null && $url_date2["day"]== null){
	$myCalendar->setDate(date('d'), date('m'), date('Y'));
}
else{
	$myCalendar->setDate($url_date2["day"], $url_date2["month"], $url_date2["year"]);
}

$myCalendar->setYearInterval(2010, 2020);
$myCalendar->dateAllow('2010-01-01', '2020-12-31', false);
$myCalendar->startMonday(true);
$myCalendar->writeScript();
?> 
<br />
<br />

<input type="submit" value="Download"
	onclick="this.form.action = 'sn_download_data.php'" /> <input
	type='hidden' name='SHOW_CHART' value='1' /> <input type="submit"
	value="Display Graph" onclick="this.form.action='sn_data_service.php'" />
</form>

<!-- LOAD FILES AND DATA -->
<?php

$date1 = $_GET["date1"];
$date2 = $_GET["date2"];
$sensor_id = $_GET["sensors"];

$start_date = get_date_format($date1);
$end_date = get_date_format($date2);
$ts1 = get_timestamp($date1);
$ts2 = get_timestamp($date2);
$days = get_days_between($ts1, $ts2);

// 0->startVal; 1->endVal; 2->step
$chart_params = get_params($date1, $date2);

$graph_data = get_graph_data($sensor_id, $start_date, $end_date, $days);
//$filtered_values = get_filtered_values($sensor_id, $start_date, $end_date, $days, $number_of_points); ///OK!!
$val = $graph_data["data_values"];
//$nodata_intervals = caculate_no_data_intervals($values);
?>

<!-- Graph section-->
<?php 
if($url_date1["year"] != null && $url_date1["month"] != null && $url_date1["day"] != null &&
$url_date2["year"] != null && $url_date2["month"] != null && $url_date2["day"] != null){
	echo "<h3>Graph</h3>";
}
?>

<?php
$lineChart = new gLineChart(700,300);
//echo sizeof($val);
$lineChart->addDataSet($val);
$lineChart->setLegend(array("CO2"));
$lineChart->setColors(array("FF6600"));
$lineChart->setVisibleAxes(array('x','y'));
$lineChart->setDataRange(0.0, 4.0);
$lineChart->addAxisRange(0, $chart_params[0], $chart_params[1], $chart_params[2]);
$lineChart->addAxisRange(1, 0.0, 4.0, 0.2);
$lineChart->setGridLines(0, 5); //LINEAS
$lineChart->addBackgroundFill('bg', 'EFEFEF');
$lineChart->addBackgroundFill('c', '000000');
?>

<?php
$isChart = $_GET['SHOW_CHART'];
if ( $isChart == '1') {
	echo'<img src="'. $lineChart->getUrl() . '">';

echo 'Vertical axis units in Volts<br/>
Horizontal axis units in hours or days(if selected period is greather than 2 days)<br/>';
}
?>

</body>
